﻿using System;
using System.Collections.Generic;
using System.Windows.Controls;
using System.Linq;
using System.Windows;
using System.Collections.ObjectModel;

namespace QuanLyDuocPham.Controllers.QuanLyHeThong.DanhMuc
{
    public class HOATCHAT_Control : IDisposable
    {
        DuocPhamDataContext db = new DuocPhamDataContext(staticClass.Conn);
        public void Dispose()
        {
            if (db != null)
            {
                db.Dispose();
                db = null;
            }
        }
        public void loadComboBox(ComboBox cmb)
        {

            cmb.Items.Clear();
            var types = db.HOATCHAT_Get_List();

            var typeorder = types.OrderBy(w => w.TEN_HOATCHAT);
            foreach (var t in typeorder)
            {
                cmb.Items.Add(t);
            }
            cmb.SelectedValuePath = "MA_HOATCHAT";
            cmb.IsTextSearchEnabled = true;
            cmb.IsTextSearchCaseSensitive = false;
            cmb.IsEditable = true;
            HOATCHAT_Get_ListResult temp = new HOATCHAT_Get_ListResult();

            temp.MA_HOATCHAT = "...";
            temp.TEN_HOATCHAT = "Thêm mới";
            cmb.Items.Add(temp);
        }
        public void loadComboBox(ListBox lb)
        {

            lb.Items.Clear();
            var types = db.HOATCHAT_Get_List();

            var typeorder = types.OrderBy(w => w.TEN_HOATCHAT);
            foreach (var t in typeorder)
            {
                ListBoxItem item = new ListBoxItem();
                item.Content = t;
                lb.Items.Add(item);
            }
            lb.SelectedValuePath = "MA_HOATCHAT";
            
            lb.IsTextSearchEnabled = true;
            lb.IsTextSearchCaseSensitive = false;
            
            HOATCHAT_Get_ListResult temp = new HOATCHAT_Get_ListResult();

            temp.MA_HOATCHAT = "...";
            temp.TEN_HOATCHAT = "Thêm mới";
            lb.Items.Add(temp);
        }
        public string loadID_moi()
        {
            string ID = "";

            int rowcount = db.HOATCHAT_Get_List().Count();
            if (rowcount == 0)
            {
                ID = "HC001";
            }
            else
            {
                for (int i = 1; i < rowcount+2; i++)
                {
                    string IDtam = "HC";
                    for (int j = 0; j < (3 - i.ToString().Length); j++)
                        IDtam = IDtam + "0";
                    IDtam = IDtam + i;
                    Boolean kq = true;
                    var data = db.HOATCHAT_Get_List();

                    foreach (var t in data)
                    {

                        if (t.MA_HOATCHAT.Trim() == IDtam.Trim())

                            kq = false;
                    }
                    if (kq == true)
                    {
                        ID = IDtam;
                        break;
                    }
                }

            }
            return ID;

        }


        public void addData(string ma_hc, TextBox txtTenHoatChat, CheckBox chbTheo_Toa, RichTextBox txtGhi_Chu, TextBox txtMoTa)
        {
            if (txtTenHoatChat.Text.Trim() == "")
                MessageBox.Show("Chưa nhập tên hoạt chất!");
            else
            {
                db.HOATCHAT_Insert(ma_hc, txtTenHoatChat.Text, chbTheo_Toa.IsChecked, txtGhi_Chu.Document.FlowDirection.ToString(), txtMoTa.Text);
                MessageBox.Show("Đã thêm thành công");
            }
        }


        public void loadData(ListView List)
        {
            List.Items.Clear();
            var types = db.HOATCHAT_Get_List();
            var typeorder = types.OrderBy(w => w.TEN_HOATCHAT);
            foreach (var t in typeorder)
            {
                List.Items.Add(t);
            }
        }

        public void loadData(DataPager dp)
        {
            ObservableCollection<Object> dpSource = new ObservableCollection<Object>();
            
            var types = db.HOATCHAT_Get_List();
            var typeorder = types.OrderBy(w => w.TEN_HOATCHAT);
            int i = 1;
            foreach (var u in typeorder)
            {
                if (u.TEN_HOATCHAT.Trim() != "")
                {
                    dpSource.Add(new { STT = i, u.MA_HOATCHAT, u.TEN_HOATCHAT, u.MO_TA, u.THEO_TOA, u.GHI_CHU });
                    i++;

                }
            }
            dp.ItemsSource = dpSource;
            dp.ItemsPerPage = 10;
        }
        public void updateData(string ma_hc, TextBox txtTenHoatChat, CheckBox chbTheo_Toa, RichTextBox txtGhi_Chu, TextBox txtMoTa)
        {
            if (txtTenHoatChat.Text.Trim() == "")
                MessageBox.Show("Chưa nhập tên hoạt chất!");
            else
            {
                db.HOATCHAT_Update(ma_hc, ma_hc, txtTenHoatChat.Text, chbTheo_Toa.IsChecked, txtGhi_Chu.Document.Blocks.ToString(), txtMoTa.Text);
                MessageBox.Show("Đã cập nhật!");
            }
        }

        public string deleteData(string ID_Delete, string error)
        {
            error = "";
            if (db.CO_HOAT_CHAT_GetByMaHoatchat(ID_Delete).Count() > 0)
                error = "Không thể xóa";
            else
            {
                db.HOATCHAT_Delete(ID_Delete);
                error = "Xóa thành công!";
            }
            return error;


        }
        public void loadForm_Edit(string ID_Edit,  TextBox txtTenHoatChat, CheckBox chbTheo_Toa, RichTextBox txtGhi_Chu, TextBox txtMoTa)
        {

            var sl = (HOATCHAT_GetByMaHoatchatResult)db.HOATCHAT_GetByMaHoatchat(ID_Edit).Single();


            
            txtTenHoatChat.Text = sl.TEN_HOATCHAT;
            chbTheo_Toa.IsChecked = sl.THEO_TOA;
            txtMoTa.Text = sl.MO_TA;
            txtGhi_Chu.AppendText( sl.GHI_CHU);
        }
    }
}
